package com.ssy.lingxi.pay.serviceimpl;

import com.ssy.lingxi.common.constant.order.OrderPayChannelEnum;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.message.api.feign.SystemMessageControllerFeign;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderPayParameterFeignVO;
import com.ssy.lingxi.order.api.model.vo.response.PaymentParameterFeignDetailVO;
import com.ssy.lingxi.pay.api.dto.request.MobilePayRequest;
import com.ssy.lingxi.pay.api.dto.request.assetAccount.BalancePayRequest;
import com.ssy.lingxi.pay.common.PayTypeEnum;
import com.ssy.lingxi.pay.model.assetAccount.PayResult;
import com.ssy.lingxi.pay.service.IFeignService;
import com.ssy.lingxi.pay.service.IWeChatPayService;
import com.ssy.lingxi.pay.service.assetAccount.IMemberAssetAccountService;
import com.ssy.lingxi.pay.utils.FeignLogUtils;
import com.ssy.lingxi.report.api.feign.ReportControllerFeign;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 异步服务
 *
 * @author 伍将
 * @version 2.0.0
 * @date 2020/11/18
 */
@Service
public class FeignServiceImpl implements IFeignService {

    @Resource
    private ReportControllerFeign reportController;

    @Resource
    private IWeChatPayService weChatPayService;

    @Resource
    private IMemberAssetAccountService memberAssetAccountService;

    private static final Logger logger = LoggerFactory.getLogger(FeignServiceImpl.class);

    @Resource
    private SystemMessageControllerFeign systemMessageControllerFeign;
    @Resource
    private OrderFeignService orderFeignService;

    /**
     * 发送系统通知
     *
     * @param request:
     * @author 伍将
     * @date 2020/12/21
     **/
    @Async
    @Override
    public void sendSystemMessage(SystemMessageRequest request) {
        try {
            systemMessageControllerFeign.sendSystemMessage(request);
        } catch (Exception e) {
            logger.info("调用报表服务新增系统通知接口失败：{}", e.getMessage());
        }
    }

    /**
     * 批量发送系统通知
     *
     * @param requestList:
     * @author 伍将
     * @date 2020/12/21
     **/
    @Async
    @Override
    public void batchSendSystemMessage(List<SystemMessageRequest> requestList) {
        try {
            systemMessageControllerFeign.batchSendSystemMessage(requestList);
        } catch (Exception e) {
            logger.info("调用报表服务批量新增系统通知接口失败：{}", e.getMessage());
        }
    }

    /**
     * 新增系统操作通知
     *
     * @param noticeVO: 请求参数
     * @author 伍将
     * @date 2021/1/12
     **/
    @Async
    @Override
    public void addSysOperateNotice(OperateNoticeVO noticeVO) {
        try {
            reportController.addOperateNotice(noticeVO);
        } catch (Exception e) {
            logger.info("调用报表服务新增操作通知接口失败：{}", e.getMessage());
        }
    }

    /**
     * 授信还款
     *
     * @param mobilePayRequest
     * @param payType
     * @param request
     * @return
     */
    @Override
    public Wrapper<String> creditWeChatRepay(MobilePayRequest mobilePayRequest, Integer payType, HttpServletRequest request) {
        try {
            mobilePayRequest.setPayType(PayTypeEnum.Member.getCode());
            PayResult payResult = weChatPayService.nativePay(mobilePayRequest, request);
            if (payResult.isSuccess()) {
                //返回支付路径
                Map<String, String> body = (Map<String, String>) payResult.getBody();
                return Wrapper.success(body.get("code_url"));
            }
            throw new BusinessException(ResponseCode.WECHAT_PAY_ERROR);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception ex) {
            throw new BusinessException(ResponseCode.WECHAT_PAY_ERROR);
        }
    }

    /**
     * 授信余额还款
     *
     * @param payRequest: 支付请求参数
     * @author 伍将
     * @date 2020/12/31
     * @return:
     **/
    @Override
    public Wrapper<String> creditBalanceRepay(BalancePayRequest payRequest) {
        return Wrapper.success(memberAssetAccountService.balancePay(payRequest));
    }

    @Override
    public Wrapper<PaymentParameterFeignDetailVO> findPaymentParameters(OrderPayParameterFeignVO payRequest) {
        Wrapper<PaymentParameterFeignDetailVO> wrapper = orderFeignService.findPaymentParameters(payRequest);
        FeignLogUtils.printLog("查询会员支付参数[授信]配置", wrapper, payRequest);
        return wrapper;
    }

    private Wrapper<PaymentParameterFeignDetailVO> findPaymentParameters(Long memberId, Long roleId) {
        OrderPayParameterFeignVO feignVO = new OrderPayParameterFeignVO();
        feignVO.setMemberId(memberId);
        feignVO.setRoleId(roleId);
        feignVO.setPayChannel(OrderPayChannelEnum.CREDIT);

        return findPaymentParameters(feignVO);
    }

    @Override
    public Boolean isCreditPayment(Long memberId, Long memberRoleId) {
        PaymentParameterFeignDetailVO creditPaymentInfo = getCreditPaymentInfo(memberId, memberRoleId);
        return creditPaymentInfo != null;
    }

    private PaymentParameterFeignDetailVO getCreditPaymentInfo(Long memberId, Long memberRoleId) {
        Wrapper<PaymentParameterFeignDetailVO> paymentParameters = findPaymentParameters(memberId, memberRoleId);
        if (paymentParameters != null && paymentParameters.getCode() == ResponseCode.SUCCESS.getCode()) {
            return paymentParameters.getData();
        }

        return null;
    }

    @Override
    public PaymentParameterFeignDetailVO getCreditPayment(Long memberId, Long memberRoleId) {
        return getCreditPaymentInfo(memberId, memberRoleId);
    }
}
